﻿using System;
using System.IO;
using VH6I_B4.ProductParsing;
using System.Collections.Generic;
using VH6I_B4.Datawarehouse;



namespace VH6I_B4.BronBestandReaders
{
    class CsvReader : ABronBestandReader
    {
        private StreamReader _srcFile;
        private string _supermarket;
        private string[] _prevRow;
        private ProductParser _productParser;

        public CsvReader(DatawarehouseApi dwh)
            : base(dwh)
        {
        }

        public override void  read()
        {
            _productParser = new ProductParser();
            _srcFile = new StreamReader(@"C:/Users/Remco Janssen/Desktop/KoffieBestandVolume.csv");

            string rawRow;

            while ((rawRow = _srcFile.ReadLine()) != null)
            {
                string[] currentRow = splitRow(rawRow);

                if (!currentRow[1].Equals(string.Empty) && _supermarket != null)
                {
                    Dictionary<ProductLevel, string> parsedProduct = _productParser.Parse(currentRow[1]);
                    if (parsedProduct.ContainsKey(ProductLevel.BARCODE))
                    {
                        // 2007
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2007, 1, strToDouble(currentRow[2]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2007, 2, strToDouble(currentRow[3]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2007, 3, strToDouble(currentRow[4]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2007, 4, strToDouble(currentRow[5]));
                        // 2008
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2008, 1, strToDouble(currentRow[6]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2008, 2, strToDouble(currentRow[7]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2008, 3, strToDouble(currentRow[8]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2008, 4, strToDouble(currentRow[9]));
                        // 2009
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2009, 1, strToDouble(currentRow[10]));
                        _dwh.SaveFact(FactType.VOLUME_SALES, parsedProduct, _supermarket, 2009, 2, strToDouble(currentRow[11]));
                    }
                }

                if (currentRow[1].Equals("CATEGORY - KOFFIE"))
                {
                    _supermarket = _prevRow[1];
                    _productParser = new ProductParser();
                }
                _prevRow = currentRow;
            }

            if (_srcFile != null)
            {
                _srcFile.Close();
            }
        }

        private string[] splitRow(string rawRow)
        {
            string[] splittedRow = rawRow.Trim().Split(';');
            for (int i = 0; i < splittedRow.Length; i++)
            {
                splittedRow[i] = splittedRow[i].Trim();
            }
            return splittedRow;
        }
    }
}


